﻿#region File Description
///////////////////////////////////////////////////////////////////////////////
/// \file GOLRulesetGeneric.cs
/// \brief This file stores Variant Life Ruleset implementations
/// \note It is generic to handle any Life-like Cellular Automation simulation
/// \note See: http://en.wikipedia.org/wiki/Life-like_cellular_automaton
/// \author Steven Duda
/// \date 28/09/2010
/// \remarks Copyright(c) 2010 LM01 Project Group (Animus Games - ViralFlux)
/// \remarks Some Rights Reserved.
/// Licenced under: Microsoft Reciprocal License (Ms-RL)
/// For More Information see: www.microsoft.com/opensource/licenses.mspx
///////////////////////////////////////////////////////////////////////////////
#endregion

#region Using Statements
using PropagationEngine;
using System.Collections.Generic;
#endregion

namespace GameOfLife
{
    /// <summary>
    /// \class GOLRuleset
    /// \brief This class simulates rules in any life-like Cellular Automation
    /// \author Steven Duda
    /// \date 28/09/2010
    /// </summary>
    internal sealed class GOLRulesetGeneric : Ruleset
    {
        #region Properties

        private List<int> birth = new List<int>();
        private List<int> survival = new List<int>();

        internal List<int> Birth
        {
            set { birth = value; }
        }
        internal List<int> Survival
        {
            set { survival = value; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// \brief Check if the given entity can transmit from source to destination node
        /// \param sourceNode Where the entity came from
        /// \param destinationNode Where the entity is going
        /// \param entity The Entity in question
        /// \return The amount of entity that spreads
        /// \author Bradley Mclain, Steven Duda
        /// \date 16/5/2010
        /// </summary>
        public override int CheckExternalSpread(Node sourceNode, Node destinationNode, Transmission transmission)
        {
            GOLNodeProperties prop = (GOLNodeProperties)sourceNode.Properties;

            //If alive allow to transmit
            return (prop.State == GOLNodeState.Alive ? 1 : 0);
        }

        /// <summary>
        /// \brief Check if the given entity can spread further within a node
        /// \param node The Node in question
        /// \return The amount of entity that spreads
        /// \author Steven Duda
        /// \date 28/09/2010
        /// </summary>
        public override int CheckInternalSpread(Node node, Transmission transmission)
        {
            //Number of recieved transmissions

            Transmission rcv = node.TransmissionsList.Find(item => item.ContainedEntity.ID == transmission.ContainedEntity.ID);

            GOLNodeProperties prop = (GOLNodeProperties)node.Properties;

            //Check if a Dead Cell is Born
            // else - Check if an Alive Cell Dies
            if (prop.State == GOLNodeState.Dead)
            {
                if (birth.Contains(rcv.Amount))
                    prop.State = GOLNodeState.Alive;
            }
            else if (!survival.Contains(rcv.Amount))
            {
                prop.State = GOLNodeState.Dead;
            }

            //Set the amount of recieved transmissions back to zero for next iteration
            rcv.Amount = 0;

            return 0;
        }

		public override void Cleanup(Node node)
        {
            //No Cleanup Required
        }

		public override void EntityInteraction(Node node)
        {
            //No Entity Interaction
        }

        #endregion
    }
}
